package utils
{
	import flash.display.DisplayObject;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.utils.getQualifiedClassName;

	public final class StageReferance
	{
		public static var stageHeight:int
		public static var stageWidth:int
		private static var _stage:Stage;
		public static var rect:Rectangle;

		/**
		 *
		 * @param $stage 舞台对象
		 * 初始化 舞台引用
		 *
		 */
		private static var _traceDisplayUnderPoint:Boolean;
		public static function set traceDisplayUnderPoint(value:Boolean):void
		{
			_traceDisplayUnderPoint = value;
			if(value)
			{
				_stage.addEventListener(MouseEvent.CLICK,onClickTrace);
			}else
			{
				_stage.removeEventListener(MouseEvent.CLICK,onClickTrace);
			}
		}
		
		public static function get traceDisplayUnderPoint():Boolean
		{
			return _traceDisplayUnderPoint;
		}
		
		private static function onClickTrace(event:MouseEvent):void
		{
			var target:DisplayObject = event.target as DisplayObject;
			while(target.parent)
			{
				trace(target);
				target = target.parent;
			}
		}
		
		public static function setup($stage:Stage):void
		{
			if (_stage != null)
				return;
			_stage=$stage;
			_stage.addEventListener(Event.EXIT_FRAME, __onNextFrame);
			_stage.addEventListener(Event.RESIZE, __onResize);
			_stage.stageFocusRect=false;
			rect=new Rectangle(0, 0, _stage.stageWidth, _stage.stageHeight);
		}
		
		private static function __onNextFrame(event:Event):void
		{
			if (_stage.stageWidth > 0)
			{
				_stage.removeEventListener(Event.EXIT_FRAME, __onNextFrame);
				stageWidth=_stage.stageWidth;
				stageHeight=_stage.stageHeight;
			}
		}

		private static function __onResize(event:Event):void
		{
			stageWidth=_stage.stageWidth;
			stageHeight=_stage.stageHeight;
			rect.width=stageWidth;
			rect.height=stageHeight;
		}

		/**
		 *
		 * @return 获取舞台
		 *
		 */
		public static function get stage():Stage
		{
			return _stage;
		}

		public function StageReferance()
		{
		}
	}
}
